package servlet;

import org.apache.commons.beanutils.BeanUtils;
import pojo.DepartInfo;
import pojo.PageInfo;
import pojo.Student;
import pojo.Teacher;
import service.DepartInfoService;
import service.PageService;
import service.StuService;
import service.TeaService;
import service.serviceImpl.DepartInfoServiceImpl;
import service.serviceImpl.PageServiceImpl;
import service.serviceImpl.StuServiceImpl;
import service.serviceImpl.TeaServiceImpl;
import utils.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TeaServlet extends BaseServlet {
    public String teaLogin(HttpServletRequest request, HttpServletResponse response)  {
        try {
            //获得用户输入的账号,密码
            String teaId = request.getParameter("name");
            String pwd = request.getParameter("password");

            System.out.println(teaId + "aaa");
            System.out.println(pwd + "aaa");

            //调用业务层查找教师
            TeaService teaService = new TeaServiceImpl();
            Teacher tea = teaService.findTeaById(teaId);
            //调用分页业务层
            PageService pageService = new PageServiceImpl();

            if(tea != null && pwd.equals(tea.getTea_password())){
                //账号和密码都正确
                request.getSession().setAttribute("tea", tea);
                System.out.println(tea.toString());
                //调用部门信息的业务层，当教师登录时，将部门相关信息加载到页面
                DepartInfoService departInfoService = new DepartInfoServiceImpl();

                List<DepartInfo> colInfo = departInfoService.findDepartInfo("1");
                List<DepartInfo> orgInfo = departInfoService.findDepartInfo("2");
                List<DepartInfo> youthInfo = departInfoService.findDepartInfo("3");
                List<DepartInfo> libInfo = departInfoService.findDepartInfo("4");
                List<DepartInfo> offInfo = departInfoService.findDepartInfo("5");
                List<DepartInfo> finaInfo = departInfoService.findDepartInfo("6");
                List<DepartInfo> manageInfo = departInfoService.findDepartInfo("7");
                List<DepartInfo> domiInfo = departInfoService.findDepartInfo("8");

                request.getSession().setAttribute("colInfo",colInfo);
                request.getSession().setAttribute("orgInfo",orgInfo);
                request.getSession().setAttribute("youthInfo",youthInfo);
                request.getSession().setAttribute("libInfo",libInfo);
                request.getSession().setAttribute("offInfo",offInfo);
                request.getSession().setAttribute("finaInfo",finaInfo);
                request.getSession().setAttribute("manageInfo",manageInfo);
                request.getSession().setAttribute("domiInfo",domiInfo);

                Map<String, String []> map = request.getParameterMap();

                PageInfo pageInfo = new PageInfo();
                BeanUtils.populate(pageInfo,map);

                for (String key : map.keySet()) {
                for (String str:map.get(key)) {
                    System.out.println(key + "==" + str);
                }
            }

                Page<Student> page = pageService.getStuByPageInfo(teaId,pageInfo);
                System.out.println("page：");
                System.out.println(page.toString());
                request.getSession().setAttribute("page",page);

                return "/jsp/teaInfo.jsp";
            }
            request.getSession().setAttribute("loginMsg","账号或密码输入有误");
        } catch (Exception e) {
            request.getSession().setAttribute("loginMsg","账号或密码输入有误");
        }
        return "/jsp/teaLogin.jsp";
    }
    public String findStuById(HttpServletRequest request, HttpServletResponse response){
        try {
            String stuId = request.getParameter("stuName");
            String teaId = request.getParameter("id");
            //调用教师业务层
            TeaService teaService = new TeaServiceImpl();

            Teacher tea = teaService.findTeaById(teaId);
            //将教师管理的学生转移到list中
            List<Student> list = new ArrayList<Student>(tea.getTeaLists());
            //将教师管理的学生清空
            tea.getTeaLists().clear();

            //在教师自己管理的学生中查找，如果有就添加到查找结果中
            for (Student stu: list) {
                if(stuId.equals(stu.getStu_id())){
                    tea.getTeaLists().add(stu);
                }
            };
            request.getSession().setAttribute("tea",tea);

            return "/jsp/teaInfo.jsp";
        } catch (SQLException e) {
            return  null;
        }
    }

    public String findStusById(HttpServletRequest request, HttpServletResponse response){
        try {

            String teaId = request.getParameter("id");
            //调用业务层
            TeaService teaService = new TeaServiceImpl();

            Teacher tea = teaService.findTeaById(teaId);

            request.getSession().setAttribute("tea",tea);

            return "/jsp/teaInfo.jsp";
        } catch (SQLException e) {
            return  null;
        }
    }
}
